package com.situ.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.entity.Category;
import com.situ.mall.entity.CategoryCount;
import com.situ.mall.entity.Product;
import com.situ.mall.entity.Result;
import com.situ.mall.mapper.CategoryMapper;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.service.ICategoryService;

@Service
public class CategoryServiceImpl implements ICategoryService{
	@Autowired
	private ProductMapper productMapper;
	@Autowired
	private CategoryMapper categoryMapper;

	@Override
	public List<Category> selectTopCategoryList() {
		return categoryMapper.selectTopCategoryList();
	}

	@Override
	public List<Category> selectSecondCategoryByTopCategoryId(Integer topCategoryId) {
		return categoryMapper.selectSecondCategoryByTopCategoryId(topCategoryId);
	}

	@Override
	public List<Category> selectSecondCategoryList() {
		return categoryMapper.selectSecondCategoryList();
	}

	@Override
	public List<CategoryCount> getCategoryCount() {
		return categoryMapper.getCategoryCount();
	}

	@Override
	public boolean insert(Category category) {
		int count = categoryMapper.insert(category);
		return count == 1 ? true : false;
	}

	@Override
	public Result selectByPage(Integer page, Integer limit) {
		// 查询一种有多少调数据
		int count = categoryMapper.getTotalCount();
		// 当前页数据
		int offset = (page -1 ) * limit;
		List<Category> list = categoryMapper.selectByPage(offset, limit);
		for (Category category : list) {
			System.out.println(category);
		}
		return Result.success("", list, count);
	}

	@Override
	public boolean deleteByPrimaryKey(int id) {
		int count = categoryMapper.deleteByPrimaryKey(id);
		return count == 1 ? true : false;
	}

	@Override
	public boolean deleteAll(Integer[] ids) {
		int count = 0;
		for (Integer id : ids) {
			// 每删除一个影响一行c返回就是1
			int c = categoryMapper.deleteByPrimaryKey(id);
			count += c;
		}
		
		//int count = productMapper.deleteAll(ids);
		return count == ids.length ? true : false;
	}

	@Override
	public Category getUpdateCategory(Integer cateId) {
		Category category = categoryMapper.selectUpdateCategory(cateId);
		System.out.println("category = " + category);
		return category;
	}

	@Override
	public boolean update(Category category) {
		int count = categoryMapper.updateByPrimaryKey(category);
		System.out.println("count = " +count);
		return count == 1 ? true : false;
	}

	@Override
	public int getTotalCountByCategoryId(Integer categoryId) {
		int count = productMapper.getTotalCountByCategoryId(categoryId);
		return count;
	}

	@Override
	public int getTotalCountByName(String name) {
		int count = productMapper.getTotalCountByLikeName(name);
		return count;
	}

}
